//
// Created by wk on 2023/12/5.
//

#include "AudioConvert.h"
#include <algorithm>

void audio::AudioConvert::convert_signed16_tof32(std::int16_t *s16leData, int inputLen, float *out) {
    for (size_t i = 0; i < inputLen; i++) {
        out[i] = static_cast<float>(s16leData[i]) / 32768.0f;
    }
}

void audio::AudioConvert::convert_f32_to_signed16(float *inputData, int inputLen, std::int16_t *out) {
    for (size_t i = 0; i < inputLen; i++)
    {
        float clampedValue = std::max<float>(-1.0f, std::min<float>(1.0f, inputData[i]));  // 将范围限制在 -1.0 到 1.0 之间
        out[i] = static_cast<std::int16_t>(clampedValue * 32767.0f);   // 将 float 范围 (-1.0 到 1.0) 映射到 S16LE 范围 (-32768 到 32767)
    }
}
